<?php
class question_class
{
    public $TABLE_NAME = 'Question';
    public $TABLE_COLUMNS = array(
                                'id' => 'Question_id',
                                'report'    =>  'id_Report_model',
                                'type'      =>  'id_Question_type',
                                'order'     =>  'Order',
                                'question'  =>  'Question',
                                'choices'   =>  'Choices',
                                'filters'   =>  'Filters'
                                );

    function __construct( $dblink)
    {
        if ( $dblink == NULL || !$dblink )
        {
            echo 'You need to specify a database valid link';
            return NULL;
        }
        $this->DB_LINK = $dblink;
    }

    function find_by( $Columns, $Values, $Limit = NULL, $Offset = NULL, $Newest = false )
    {
        $select_sql = sprintf( "
            SELECT
                *
            FROM
                `%s`
            WHERE
               ",
                $this->TABLE_NAME
                );
        $error = false;

        if ( is_array( $Columns ) && is_array( $Values ) )
        {
            if ( ( $condition_number = count( $Columns ) ) && ( count( $Columns ) == count( $Values ) ) )
            {
                for( $i = 0 ; $i < $condition_number ; $i++ )
                {
                    if ( $i > 0 )
                    {
                        $select_sql .= " AND ";
                    }
                    $select_sql .= sprintf(
                            " `%s`=?  ",
                            $this->TABLE_COLUMNS[$Columns[$i]]
                            );
                }
                if ( $Newest )
                {
                    $select_sql .= sprintf( "ORDER BY `%s` DESC", $this->TABLE_COLUMNS['id'] );
                }
                if ( $Limit > 0 )
                {
                    if ( $Offset > 0 )
                    {
                        $select_sql .= sprintf( " LIMIT %d,%d", $Offset, $Limit );
                    }
                    else
                    {
                        $select_sql .= sprintf( " LIMIT %d", $Limit );
                    }
                }
                $select_result = $this->DB_LINK->Execute( $select_sql, $Values );
            }
            else
            {
                $error = true;
            }

        }
        elseif ( !is_array( $Columns ) && !is_array( $Values ) )
        {
            $select_sql = sprintf( "
                SELECT
                    *
                FROM
                    `%s`
                WHERE
                    `%s`=? ",
                    $this->TABLE_NAME,
                    $this->TABLE_COLUMNS[ $Columns ]
                    );
            if ( $Newest )
            {
                $select_sql .= sprintf( "ORDER BY `%s` DESC", $this->TABLE_COLUMNS['id'] );
            }
            if ( $Limit > 0 )
            {
                if ( $Offset > 0 )
                {
                    $select_sql .= sprintf( " LIMIT %d,%d", $Offset, $Limit );
                }
                else
                {
                    $select_sql .= sprintf( " LIMIT %d", $Limit );
                }
            }
            $select_result = $this->DB_LINK->Execute( $select_sql, array( $Values ) );
        }
        else
        {
            $error = true;
        }

        if ( $select_result && ( $error == false ) )
        {
            $questions_returned = $select_result->GetRows();

            foreach( $questions_returned as &$q )
            {
                if ( $q[$this->TABLE_COLUMNS['choices']] )
                {
                    $q[$this->TABLE_COLUMNS['choices']] = json_decode( $q[$this->TABLE_COLUMNS['choices']] );
                }
                if ( $q[$this->TABLE_COLUMNS['filters']] )
                {
                    $q[$this->TABLE_COLUMNS['filters']] = json_decode( $q[$this->TABLE_COLUMNS['filters']] );
                }
            }
            return $questions_returned;
        }
        else
        {
            return array();
        }
    }

    function modify_data_for_id( $Id, $Data )
    {
        if ( $Id <= 0 )
        {
            return false;
        }

        $modify_user_data_sql = sprintf( "UPDATE
                                            `%s`
                                          SET
                                          ", $this->TABLE_NAME );

        $values = array();
        foreach( $Data as $key=>$c )
        {
            $values[] = $c;
            $modify_user_data_sql .= sprintf( " `%s` = ? , ", $this->TABLE_COLUMNS[ $key ] );
        }
        $modify_user_data_sql = substr( $modify_user_data_sql, 0, -2 );
        $modify_user_data_sql .= sprintf( "WHERE
                                            `%s`= ? ", $this->TABLE_COLUMNS[ 'id' ]
            );
        $values[] = $Id;

        $modify_result = $this->DB_LINK->Execute( $modify_user_data_sql, $values );
        if ( !$modify_result )
        {
            return $this->DB_LINK->ErrorMsg();
        }
        else
        {
            return TRUE;
        }

        return TRUE;
    }

    function get_all()
    {
        $get_all_sql = sprintf( "SELECT * FROM `%s`", $this->TABLE_NAME );
        $get_all_result = $this->DB_LINK->GetAll( $get_all_sql );
        if ( $get_all_result )
        {
            return $get_all_result;
        }
        else
        {
            return false;
        }
    }

    function add( $Report_model_id, $Data )
    {
        if ( $Report_model_id <= 0 )
        {
            return false;
        }

        $insert_sql = sprintf( "
                INSERT INTO
                    `%s`
                (
                    `%s`, %%s
                )
                VALUES
                (
                    ?, %%s
                )
            ",
                $this->TABLE_NAME,
                $this->TABLE_COLUMNS['report']
                );

        $columns = array();
        $columns_string = null;

        $values = array( $Report_model_id );
        $values_string = null;

        foreach( $Data as $k=>$d )
        {
            $columns_string .= sprintf( "`%s`, ", $this->TABLE_COLUMNS[$k] );

            $values_string .= sprintf( "?, " );

            $values[] = $d;
        }
        $columns_string = substr( $columns_string, 0, -2 );
        $values_string = substr( $values_string, 0, -2 );

        $insert_sql = sprintf(
                $insert_sql,
                $columns_string,
                $values_string
                );

        $insert_result = $this->DB_LINK->Execute( $insert_sql, $values );
        $insert_id = $this->DB_LINK->Insert_ID();
        if ( $insert_id )
        {
            return $insert_id;
        }

        return false;
    }


}
?>